package com.feng.security.mac;

import java.security.NoSuchAlgorithmException;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;

public class MACCoder {
	public static byte[] initHmacMD5Key() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密钥产生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacMD5");
		// 产生密钥
		SecretKey secretKey = generator.generateKey();
		// 获得密钥
		byte[] key = secretKey.getEncoded();
		return key;
	}

	public static String encodeHmacMD5(byte[] data, byte[] key) throws Exception {
		// 还原密钥
		SecretKey secretKey = new SecretKeySpec(key, "HmacMD5");
		// 实例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 执行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
	}

	public static byte[] initHmacSHAKey() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密钥产生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA1");
		// 产生密钥
		SecretKey secretKey = generator.generateKey();
		// 获得密钥
		byte[] key = secretKey.getEncoded();
		return key;
	}

	public static String encodeHmacSHA(byte[] data, byte[] key) throws Exception {
		// 还原密钥
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA1");
		// 实例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 执行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
	}

	public static byte[] initHmacSHA256Key() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密钥产生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA256");
		// 产生密钥
		SecretKey secretKey = generator.generateKey();
		// 获得密钥
		byte[] key = secretKey.getEncoded();
		return key;
	}

	public static String encodeHmacSHA256(byte[] data, byte[] key) throws Exception {
		// 还原密钥
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA256");
		// 实例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 执行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
	}

	public static byte[] initHmacSHA384Key() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密钥产生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA384");
		// 产生密钥
		SecretKey secretKey = generator.generateKey();
		// 获得密钥
		byte[] key = secretKey.getEncoded();
		return key;
	}

	public static String encodeHmacSHA384(byte[] data, byte[] key) throws Exception {
		// 还原密钥
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA384");
		// 实例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 执行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
	}

	public static byte[] initHmacSHA512Key() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密钥产生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA512");
		// 产生密钥
		SecretKey secretKey = generator.generateKey();
		// 获得密钥
		byte[] key = secretKey.getEncoded();
		return key;
	}

	public static String encodeHmacSHA512(byte[] data, byte[] key) throws Exception {
		// 还原密钥
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA512");
		// 实例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 执行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
	}

	/**
	 * BouncyCastle的实现和应用第三方加密组件包Bouncy
	 * Castle作为补充，提供了HmacMD2.HmacMD4、HmacSHA224三种算法的支持。
	 * import org.bouncycastle.jce.provider.BouncyCastleProvider;  
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
//	public static byte[] initHmacMD2Key() throws NoSuchAlgorithmException {
//		// 添加BouncyCastle的支持
//		Security.addProvider(new BouncyCastleProvider());
//		// 初始化HmacMD5摘要算法的密钥产生器
//		KeyGenerator generator = KeyGenerator.getInstance("HmacMD2");
//		// 产生密钥
//		SecretKey secretKey = generator.generateKey();
//		// 获得密钥
//		byte[] key = secretKey.getEncoded();
//		return key;
//	}
//
//	public static String encodeHmacMD2(byte[] data, byte[] key) throws Exception {
//		// 添加BouncyCastle的支持
//		Security.addProvider(new BouncyCastleProvider());
//		// 还原密钥
//		SecretKey secretKey = new SecretKeySpec(key, "HmacMD2");
//		// 实例化Mac
//		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
//		// 初始化mac
//		mac.init(secretKey);
//		// 执行消息摘要
//		byte[] digest = mac.doFinal(data);
//		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
//	}
//
//	public static byte[] initHmacMD4Key() throws NoSuchAlgorithmException {
//		// 添加BouncyCastle的支持
//		Security.addProvider(new BouncyCastleProvider());
//		// 添加BouncyCastle的支持
//		Security.addProvider(new BouncyCastleProvider());
//		// 初始化HmacMD5摘要算法的密钥产生器
//		KeyGenerator generator = KeyGenerator.getInstance("HmacMD4");
//		// 产生密钥
//		SecretKey secretKey = generator.generateKey();
//		// 获得密钥
//		byte[] key = secretKey.getEncoded();
//		return key;
//	}
//
//	public static String encodeHmacMD4(byte[] data, byte[] key) throws Exception {
//		// 添加BouncyCastle的支持
//		Security.addProvider(new BouncyCastleProvider());
//		// 还原密钥
//		SecretKey secretKey = new SecretKeySpec(key, "HmacMD4");
//		// 实例化Mac
//		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
//		// 初始化mac
//		mac.init(secretKey);
//		// 执行消息摘要
//		byte[] digest = mac.doFinal(data);
//		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
//	}
//
//	public static byte[] initHmacSHA224Key() throws NoSuchAlgorithmException {
//		// 添加BouncyCastle的支持
//		Security.addProvider(new BouncyCastleProvider());
//		// 添加BouncyCastle的支持
//		Security.addProvider(new BouncyCastleProvider());
//		// 初始化HmacMD5摘要算法的密钥产生器
//		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA224");
//		// 产生密钥
//		SecretKey secretKey = generator.generateKey();
//		// 获得密钥
//		byte[] key = secretKey.getEncoded();
//		return key;
//	}
//
//	public static String encodeHmacSHA224(byte[] data, byte[] key) throws Exception {
//		// 添加BouncyCastle的支持
//		Security.addProvider(new BouncyCastleProvider());
//		// 还原密钥
//		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA224");
//		// 实例化Mac
//		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
//		// 初始化mac
//		mac.init(secretKey);
//		// 执行消息摘要
//		byte[] digest = mac.doFinal(data);
//		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
//	}

	public static void main(String[] args) throws Exception {
		String testString = "asdasd";

		byte[] keyHmacMD5 = MACCoder.initHmacMD5Key();
		System.out.println(MACCoder.encodeHmacMD5(testString.getBytes(), keyHmacMD5));

		byte[] keyHmacSHA1 = MACCoder.initHmacSHAKey();
		System.out.println(MACCoder.encodeHmacSHA(testString.getBytes(), keyHmacSHA1));

		byte[] keyHmacSHA256 = MACCoder.initHmacSHA256Key();
		System.out.println(MACCoder.encodeHmacSHA256(testString.getBytes(), keyHmacSHA256));

		byte[] keyHmacSHA384 = MACCoder.initHmacSHA384Key();
		System.out.println(MACCoder.encodeHmacSHA384(testString.getBytes(), keyHmacSHA384));

		byte[] keyHmacSHA512 = MACCoder.initHmacSHA512Key();
		System.out.println(MACCoder.encodeHmacSHA512(testString.getBytes(), keyHmacSHA512));
		
		/*byte[] keyHmacMD2 = MACCoder.initHmacMD2Key();  
        System.out.println(MACCoder.encodeHmacMD2(testString.getBytes(), keyHmacMD2));  
  
        byte[] keyHmacMD4 = MACCoder.initHmacMD4Key();  
        System.out.println(MACCoder.encodeHmacMD4(testString.getBytes(), keyHmacMD4));  
  
        byte[] keyHmacSHA224 = MACCoder.initHmacSHA224Key();  
        System.out.println(MACCoder.encodeHmacSHA224(testString.getBytes(), keyHmacSHA224));  */
	}

}